package javaCode.chapter13.list;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class ListExercise02 {
    public static void main(String[] args) {

        /*
        使用List的实现类添加三本图书，并遍历，打印如下效果

        名称:xx   价格:xx   作者:xx
        名称:xx   价格:xx   作者:xx
        要求
        1. 按价格排序，从低到高(使用冒泡法)
        2. 要求使用 ArrayList、LinkedList和 Vector 三种集合实现
         */
        List list = new ArrayList();
        list.add(new Book("西游记", 80.0, "吴承恩"));
        list.add(new Book("红楼梦", 55.9, "曹雪芹"));
        list.add(new Book("Java高级设计", 99.9, "韩顺平"));

        sort(list);

        for (Object obj : list) {
            System.out.println("obj=" + obj);
        }
    }

    public static void sort(List list) {
        int listSize = list.size();
        for (int i = 0; i < listSize - 1; i++) {
            for (int j = 0; j < listSize - i - 1; j++) {
                //取出对象Book
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j + 1);
                if (book1.getPrice() > book2.getPrice()){
                    list.set(j,book2);
                    list.set(j+1,book1);
                }
            }
        }
    }
}

class Book {
    private String name;
    private double price;
    private String author;

    public Book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "名称:" + name + "\t价格:" + price + "\t作者:" + author;
    }
}